import 'package:basic_common/app_theme.dart';
import 'package:basic_common/common_exports.dart';
import 'package:basic_common/global_getx.dart';
import 'package:basic_widgets/app_bar_widget.dart';
import 'package:basic_widgets/button_widget.dart';
import 'package:basic_widgets/image_widget.dart';
import 'package:basic_widgets/text_widget.dart';
import 'profile_controller.dart';

class ProfilePage extends GetView<ProfileController> {
  final controller=Get.put<ProfileController>(ProfileController());
  final globalGetx=Get.put<GlobalGetx>(GlobalGetx());

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: mainAppbar(title: "我的", actions: [
        Padding(
          padding: EdgeInsets.only(right: ScreenHelper.width(15)),
          child: GestureDetector(
            onTap: () => Get.toNamed(AppRoutes.qrscan),
            child: Image.asset(
              "assets/profile/icon_scan.png",
              width: ScreenHelper.width(24),
              height: ScreenHelper.width(24),
              package: "basic_common",
            ),
          ),
        )
      ]),
      body: SingleChildScrollView(
        physics: const AlwaysScrollableScrollPhysics(),
        primary: false,
        child: ListView(
          physics: const NeverScrollableScrollPhysics(),
          shrinkWrap: true, //收缩
          children: [
            // 头部
            buildHeadInfo(),
            // 菜单信息
            buildMenuWidget(),
            buildSetting()
          ],
        ),
      ),
    );
  }

  /// 头部内容显示
  Widget buildHeadInfo() {
    return Stack(
      children: [
        GestureDetector(
          onTap: () => {Get.toNamed(AppRoutes.personal)},
          child: Container(
              alignment: Alignment.topCenter,
              // height: ScreenHelper.height(100),
              color: Colors.white,
              margin: EdgeInsets.symmetric(horizontal: ScreenHelper.width(15)),
              child: GetBuilder<GlobalGetx>(
                id: 'doctorInfo',
                builder: (controller) => ListTile(
                  isThreeLine: false,
                  leading: Stack(
                    children: [
                      circleImg(
                          width: ScreenHelper.width(50),
                          head: globalGetx.doctorInfo != null &&
                                  globalGetx.doctorInfo!.profilePhoto != null
                              ? globalGetx.doctorInfo!.profilePhoto!
                              : ''),
                      Container(
                          margin: const EdgeInsets.only(top: 40),
                          child: Image.asset(
                            globalGetx.doctorInfo != null
                                ? "assets/profile/icon_authed.png"
                                : "assets/profile/icn_unauth.png",
                            package: "basic_common",
                            width: ScreenHelper.width(51),
                          ))
                    ],
                  ),
                  title: Container(
                    // padding: EdgeInsets.only(top: 4),
                    height: ScreenHelper.height(40),
                    // color: Colors.red,
                    alignment: Alignment.topLeft,
                    child: Row(
                      mainAxisSize: MainAxisSize.min,
                      mainAxisAlignment: MainAxisAlignment.start,
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Container(
                            alignment: Alignment.topCenter,
                            padding: const EdgeInsets.only(top: 8),
                            // color: Colors.red,
                            child: Text(
                              globalGetx.doctorInfo != null
                                  ? globalGetx.doctorInfo!.doctorNick!
                                  : '',
                              style: TextStyle(
                                  color: AppTheme.dartTextColor,
                                  fontSize: ScreenHelper.sp(16)),
                              textAlign: TextAlign.center,
                            )),
                        TextButton.icon(
                            onPressed: null,
                            icon: Image.asset(
                              globalGetx.identityInfo != null &&
                                      globalGetx.identityInfo!.gender == '1'
                                  ? "assets/profile/icon_sex_male.png"
                                  : 'assets/profile/icon_female.png',
                              width: ScreenHelper.width(14),
                              height: ScreenHelper.width(14),
                              package: "basic_common",
                            ),
                            label: Text(
                              globalGetx.doctorInfo != null
                                  ? globalGetx.doctorInfo!.titleName ?? ''
                                  : "",
                              style: TextStyle(
                                  color: AppTheme.weakTextColor,
                                  fontSize: ScreenHelper.sp(14)),
                              textAlign: TextAlign.left,
                            )),
                      ],
                    ),
                  ),
                  subtitle: Padding(
                    padding: const EdgeInsets.only(top: 0),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.start,
                      mainAxisSize: MainAxisSize.min,
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Text(
                          globalGetx.doctorInfo != null
                              ? globalGetx.doctorInfo!.dutyName ?? ''
                              : '',
                          textAlign: TextAlign.start,
                          style: TextStyle(
                              color: AppTheme.weakTextColor,
                              fontSize: ScreenHelper.sp(13)),
                        ),
                        SizedBox(
                          width: ScreenHelper.width(10),
                        ),
                        Text(
                          globalGetx.doctorInfo != null
                              ? globalGetx.doctorInfo!.hospitalName!
                              : '',
                          textAlign: TextAlign.start,
                          style: TextStyle(
                              color: AppTheme.weakTextColor,
                              fontSize: ScreenHelper.sp(13)),
                        ),
                      ],
                    ),
                  ),
                  trailing: Image.asset(
                    "assets/profile/icon_arrow_next.png",
                    width: ScreenHelper.width(18),
                    package: "basic_common",
                  ),
                ),
              )),
        )
      ],
    );
  }

  /// 医生身份标签项
  buildIdenity({required String tag}) {
    return tag.isEmpty
        ? SizedBox.shrink()
        : Container(
            alignment: Alignment.center,
            padding: EdgeInsets.symmetric(horizontal: ScreenHelper.width(6)),
            // margin: EdgeInsets.symmetric(horizontal: ScreenHelper.width(8)),
            decoration: BoxDecoration(
                border: Border.all(color: AppTheme.primyColor),
                borderRadius: BorderRadius.circular(6)),
            child: Text(
              tag,
              style: TextStyle(
                  color: AppTheme.primyColor, fontSize: ScreenHelper.sp(12)),
            ),
          );
  }

  /// 统计信息
  /// 顶部内容Item项
  /// title 标题名称
  /// content 内容数据
  Widget buildTopItem({required String title, required String content}) {
    TextStyle titleStyle = TextStyle(
      fontSize: ScreenHelper.sp(12),
      color: AppTheme.dartTextColor,
    );
    TextStyle contentStyle = TextStyle(
        fontSize: ScreenHelper.sp(18),
        color: AppTheme.dartTextColor,
        fontWeight: FontWeight.bold);
    return Column(
      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
      crossAxisAlignment: CrossAxisAlignment.center,
      children: [
        Text(
          content,
          style: contentStyle,
          textAlign: TextAlign.center,
        ),
        const SizedBox(height: 10),
        Text(
          title,
          style: titleStyle,
          textAlign: TextAlign.center,
        ),
      ],
    );
  }

  /// 菜单
  buildMenuWidget() {
    return Container(
      margin: EdgeInsets.symmetric(horizontal: ScreenHelper.width(15)),
      decoration: const BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.all(Radius.circular(10))),
      child: GridView.builder(
        physics: const NeverScrollableScrollPhysics(),
        shrinkWrap: true,
        gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
          childAspectRatio: 0.8,
          crossAxisCount: 4,
          crossAxisSpacing: 10,
        ),
        itemBuilder: (context, index) {
          final itemData = controller.menuList[index];
          return GestureDetector(
            onTap: () {
              if (itemData['page'] == AppRoutes.myAttention) {
                Get.toNamed(itemData['page']!, arguments: {
                  "hosNo": globalGetx.doctorInfo!.hospNo,
                  "title": itemData['title']!
                });
              }
              Get.toNamed(itemData['page']!);
            },
            child: buildTopImgItem(
              icon: itemData['icon']!,
              title: itemData['title']!,
              packageName: "basic_common",
              width: 36,
            ),
          );
        },
        itemCount: controller.menuList.length,
      ),
    );
  }

  /// 设置列表
  Widget buildSetting() {
    return Container(
      margin: EdgeInsets.symmetric(
          horizontal: ScreenHelper.width(15), vertical: ScreenHelper.width(10)),
      decoration: BoxDecoration(
          color: Colors.white, borderRadius: BorderRadius.circular(8)),
      child: ListView.builder(
        physics: const NeverScrollableScrollPhysics(),
        shrinkWrap: true,
        itemBuilder: (context, index) {
          var item = controller.settingList[index];
          return buildItem(
            icon: item['icon']!,
            title: item['title']!,
            page: item['page']!,
          );
        },
        itemCount: controller.settingList.length,
      ),
    );
  }

  /// 设置Item项目profile
  buildItem(
      {required String icon, required String title, required String page}) {
    return GestureDetector(
      onTap: () {
        Get.toNamed(page);
      },
      child: ListTile(
        leading: Image.asset(
          icon,
          width: ScreenHelper.width(30),
          height: ScreenHelper.width(30),
          package: "basic_common",
        ),
        title: norTitle(title: title),
        trailing: Image.asset(
          "assets/profile/icon_arrow_next.png",
          width: ScreenHelper.width(18),
          package: "basic_common",
        ),
      ),
    );
  }
}
